//
//  DropControl.m
//  Weibo
//
//  Created by 千锋 on 15/12/30.
//  Copyright © 2015年 abc. All rights reserved.
//

#import "DropControl.h"

typedef NS_ENUM(NSInteger,HideType) {
    HideTypeFromSelf,     //点击自己消失
    HideTypeFromTableView //点击tableView消失

};


static CGFloat cellHeight = 50;
static NSString * dropCellID = @"DropCellID";

@interface DropControl ()<UITableViewDataSource,UITableViewDelegate>

// 存储父视图
@property(nonatomic,weak) UIView *sView;

@property(nonatomic,strong) UIImageView * dropImageView;

@property (nonatomic,assign) CGRect imgFrame;

@property(nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) NSArray * dataArray;

@end

@implementation DropControl

-(instancetype)initWithInsideViewFrame:(CGRect)frame inView:(UIView *)view dataSource:(NSArray *)dataArray{
    
    
    self = [super init];
    if (self) {
        // 将父视图存储下来
        self.sView = view;
        // 设置自身的大小和位置
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor orangeColor];
        
        //将传入的内部的imgframe记录下来
        self.imgFrame = frame;
        //记录传入数据
        self.dataArray = [dataArray copy];
        
        // 给自己添加一个点击事件
        [self addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchDown];
        
        // 初始化子视图
        self.dropImageView = [[UIImageView alloc]init];
        [self addSubview:self.dropImageView];
        self.dropImageView.image = [[UIImage imageNamed:@"popover_background"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
        
        CGRect tableRect = frame;
        tableRect.origin.x = 5;
        tableRect.origin.y = 10;
        tableRect.size.width -= 10;
        tableRect.size.height -= 17;
        self.dropImageView.userInteractionEnabled = YES;
        self.tableView = [[UITableView alloc]initWithFrame:tableRect style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.dropImageView addSubview:self.tableView];
        
        
        //再设置一个中间变量，用来计算tableView需要的高度
        CGRect absluteRect = self.tableView.frame;
        CGFloat tbHeeight = self.dataArray.count * cellHeight > absluteRect.size.height -17? absluteRect.size.height -17: self.dataArray.count * cellHeight;
        absluteRect.size.height = tbHeeight;
        self.tableView.frame = absluteRect;
    }
    return self;
}
-(void)hide{
    [self hideWithtype:HideTypeFromSelf withIndexPath:-1];
}
-(void)hideWithtype:(HideType)type withIndexPath:(NSInteger )index{
    
    // 用一个中间变量记录下img的frame
    CGRect rect = self.imgFrame;
    rect.size.height = 0;
    [UIView animateWithDuration:0.5 animations:^{
        
        self.dropImageView.frame = rect;
    } completion:^(BOOL finished) {
        self.dropImageView.userInteractionEnabled = YES;
        [self dismiss];
        switch (type) {
            case HideTypeFromSelf:
                if (_afhBlk) {
                    _afhBlk();
                }
                break;
                
            case HideTypeFromTableView:
                if (_dsBlk) {
                    _dsBlk(index);
                }
                if (_afhBlk) {
                    _afhBlk();
                }
                break;
        }
        
    }];

}
// 显示
-(void)show{
    [self.sView addSubview:self];
    // 用一个中间变量记录下img的frame
    CGRect rect = self.imgFrame;
    // 将变量rect的高度为0
    rect.size.height = 0;
    //设置dorpImagView的frame，此时他的高度为0，所以显示的不全
    self.dropImageView.frame = rect;
    
    //再设置一个中间变量，用来计算tableView需要的高度
    CGRect absluteRect = self.tableView.frame;
    CGFloat tbHeeight = self.dataArray.count * cellHeight > absluteRect.size.height ? absluteRect.size.height : self.dataArray.count * cellHeight;
    rect.size.height = tbHeeight + 22
    ;
    
    //用动画，改变dropImagView的高度

    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.dropImageView.frame = rect;
    } completion:^(BOOL finished) {
        self.userInteractionEnabled = YES;
    }];
    

}

// 消失
-(void)dismiss{
    [self removeFromSuperview];
    
}

#pragma mark - delgeate/d
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return cellHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dropCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:dropCellID];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 反选
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self hideWithtype:HideTypeFromTableView withIndexPath:indexPath.row];
    
}












@end
